//
//  EeSimpleNumField.swift
//  SmoSky
//
//  Created by zhu xietong on 2018/5/30.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import Eelay

public class EeSimpleNumStepControl1: EeSimpleNumStepControl {
    
    public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        self.bsui.set(radius: 4, width: TP.separatorHeight, color: ._separator)
    }
    public override func addLayoutRules() {
        let numB = UIView()
        self.eelay = [
            [numB,[ee.X],[ee.T.B],30],
            [reduceBt,[numT,ee.L,ee.R,-TP.separatorHeight],[ee.T.L.B],30],
            [addBt,[numT,ee.R,ee.L,TP.separatorHeight],[ee.T.L.B],30],
            [numT,[ee.X],[ee.T.B],50],
            
        ]
        numT.text = "1"
        
        let color = UIColor.clear
        numB.backgroundColor = color
        numT.textAlignment = .center
        numT.backgroundColor = ._cellBackground
        reduceBt.backgroundColor = ._cellBackground
        addBt.backgroundColor = ._cellBackground
        addBt.ui.font(size: 15).ncolor(color: ._foreground).ntitle(title: "+")
        reduceBt.ui.font(size: 15).ncolor(color: ._foreground).ntitle(title: "-")
        //            self.backgroundColor = UIColor(shex:"#aaa")
        self.bsui.set(radius: 4, width: TP.separatorHeight, color: ._separator)
        
        _ = numT.ui.font13.text(color: ._foreground).center
        numT.tintColor = ._tint
        addBt.tag = 1
        reduceBt.tag = -1
        
        addBt.addTarget(self, action: #selector(updateNum(sender:)), for: .touchUpInside)
        reduceBt.addTarget(self, action: #selector(updateNum(sender:)), for: .touchUpInside)
    }
    override public var intrinsicContentSize: CGSize{
        return [110,30]
    }
    
}

open class EeSimpleNumStepControl:FieldView{
    public var limit = NumLimit()
    {
        didSet{
            let newValue = mo.model[int:FK.value,0]
            reduceBt.isEnabled = true
            addBt.isEnabled = true
            if newValue > (limit.max)
            {
                addBt.isEnabled = false
            }
            
            if newValue < (limit.min)
            {
                reduceBt.isEnabled = false
                //            self.minAlert.alert()
            }
        }
    }
    public var value:Int = 1
    {
        didSet{
            numT.text = "\(value)"
        }
    }
    
    public var numT = UILabel()
    public let addBt = UIButton()
    public let reduceBt = UIButton()
    
    public var changeNumValue:(Int) ->Void = {_ in}
    open override func addLayoutRules() {
        let numB = UIView()
        self.eelay = [
            [numB,[ee.X],[ee.T.B],30],
            [numT,[ee.X.Y],30],
            [reduceBt,[numT,ee.L,ee.R,-1],[ee.T.L.B],30],
            [addBt,[numT,ee.R,ee.L,1],[ee.T.L.B],30],
        ]
        numT.text = "1"
        
        let color = UIColor.clear
        numB.backgroundColor = color
        numT.textAlignment = .center
        numT.backgroundColor = color
        reduceBt.backgroundColor = color
        addBt.backgroundColor = color
        addBt.ui.font(size: 14).ncolor(color: ._foreground).ntitle(title: "+")
        reduceBt.ui.font(size: 14).ncolor(color: ._foreground).ntitle(title: "-")
        //            self.backgroundColor = UIColor(shex:"#aaa")
        self.bsui.set(radius: 0, width: TP.separatorHeight, color: ._foregroundWeak)
        
        _ = numT.ui.font12.text(color: ._tint).center
        numT.tintColor = UIColor._tint
        addBt.tag = 1
        reduceBt.tag = -1
        
        addBt.addTarget(self, action: #selector(updateNum(sender:)), for: .touchUpInside)
        reduceBt.addTarget(self, action: #selector(updateNum(sender:)), for: .touchUpInside)
    }
    
    
    open override func mo_model_reload() {
        limit.min = mo.model[int:FK.min_num,0]
        limit.max = mo.model[int:FK.max_mum,0]
        limit.minMask = mo.model[FK.min_num_mark,""]
        limit.maxMask = mo.model[FK.max_mum_mark,""]
        self.value = mo.model[int:FK.value,0]
    }
    
    
    @objc open func updateNum(sender:UIButton) {
//        print("+++____S_S_S|\(sender)")
        let num = sender.tag
        let newValue = self.value + num
        reduceBt.isEnabled = true
        addBt.isEnabled = true
        if newValue > (limit.max)
        {
            if limit.maxMask.len > 0
            {
                self.limit.maxMask.alert()
            }
//            addBt.isEnabled = false
        }
        if newValue < (limit.min)
        {
//            reduceBt.isEnabled = false
            //            self.minAlert.alert()
        }
        if (newValue >= limit.min) && (newValue<=limit.max){
            self.value = newValue
            self.changeNumValue(newValue)
        }
        //        self.willSetNewValue(newValue: newValue)
    }
    override open var intrinsicContentSize: CGSize{
        return [90,30]
    }
    
}

open class EeSimpleNumField<NumType:EeSimpleNumStepControl>: FieldView {
    
    open var numControl:NumType = NumType()
    
    open override func addLayoutRules() {
        eelay = [
            [numControl,[ee.T.L.B]],
            [fieldNameL,[ee.R.Y],[numControl,ee.R,ee.L,8]],
        ]
        
    }
    
    
    open override func didLayoutRules() {
        fieldNameL.ui.font(font: valueFont).text(color: valueColor).text(place_holder)
        fieldNameL.setContentConstrainToHighLever()
        numControl.setContentConstrainToHighLever()
        numControl.changeNumValue = {[weak self] (num) in
            let value = "\(num)"
            self?.mo.model[FK.value] = value
            self?.save(value)
        }
    }
    
    open override func mo_model_reload() {
        fieldNameL.text = place_holder
        numControl.mo.model = mo.model
        self.save(mo.model[FK.value,""])
    }
    
    
    
    
    
    
    
}




